<?php

$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

DB::connection()->disableQueryLog();

$c_date=date('Y-m-d',time()-86400);//前一天

//构造 pos id 为键 支付信息为value的数组 便于后面调用
$mdb_arr=array();
$mdb_arr_amount=array();
$mdb = DB::table("maintenance_pos_device")->select("id","shop_id","device_no","code","device_name")->where("status",1)->get();
foreach ($mdb as $_mdb) {
	$mdb_arr[$_mdb->device_no]=$_mdb;
    $mdb_arr_amount[$_mdb->device_no]=array("ye_amount"=>0,"pos_amount"=>0,"posdz_amount"=>0);
}
unset($mdb);

//循环处理获取的 当天的pos记录 ，第二天获取前一天的 pos 记录创建时间按照 推后一天过滤
$start_time=date('Y-m-d 00:00:00',strtotime($c_date)+86400);
$end_time=date('Y-m-d 23:59:59',strtotime($c_date)+86400);
$db_amount=DB::table("wpos_finance_detail")->
	leftJoin("wpos_record","wpos_finance_detail.orderno","=","wpos_record.cashier_trade_no")->
	leftJoin("acount_user_recharge_record","acount_user_recharge_record.id","=","wpos_record.buss_id")->
	leftJoin("order_pay_record","order_pay_record.id","=","wpos_record.buss_id")->
	leftJoin("car_wash_order_pay_record","car_wash_order_pay_record.id","=","wpos_record.buss_id")->
	leftJoin("service_card_order_pay_record","service_card_order_pay_record.id","=","wpos_record.buss_id")->
	leftJoin("receivable_pay_record","receivable_pay_record.id","=","wpos_record.buss_id")->
	select("wpos_finance_detail.orderno","wpos_finance_detail.thirdChannelSerialno","wpos_finance_detail.batchno","wpos_finance_detail.vouchno","wpos_finance_detail.refno","wpos_finance_detail.status","wpos_finance_detail.statusText","wpos_finance_detail.payModeName","wpos_finance_detail.cardIssuresNo","wpos_finance_detail.payCash","wpos_finance_detail.refundAmount","wpos_finance_detail.incomeAmt","wpos_finance_detail.deviceEn","wpos_record.buss_id","wpos_record.shop_id","wpos_record.type","wpos_record.id as wpos_record_id","wpos_record.trade_status as wpos_record_trade_status","wpos_record.pay_info","wpos_record.buyer","wpos_record.cardType","wpos_record.amount as own_pay_amount","acount_user_recharge_record.id as aur_order_id","acount_user_recharge_record.implement_time as aur_implement_time","acount_user_recharge_record.status as aur_status","order_pay_record.order_id as o_order_id","order_pay_record.implement_time as o_implement_time","order_pay_record.status as o_status","car_wash_order_pay_record.order_id as co_order_id","car_wash_order_pay_record.implement_time as co_implement_time","car_wash_order_pay_record.status as co_status","service_card_order_pay_record.order_id as so_order_id","service_card_order_pay_record.implement_time as so_implement_time","service_card_order_pay_record.status as so_status","receivable_pay_record.receivable_id as r_order_id","receivable_pay_record.implement_time as r_implement_time","receivable_pay_record.status as r_status")->where("wpos_finance_detail.created_at",">",$start_time)->where("wpos_finance_detail.created_at","<=",$end_time);
$db_amount->chunk(1000, function($db_arr) use ($mdb_arr,$c_date,&$mdb_arr_amount) {
    foreach ($db_arr as $db) {
    	$fr=FinanceReconciliations::where("orderno",$db->orderno)->first();
    	if(is_null($fr)){
    		$fr=new FinanceReconciliations();
    	}
    	//处理业务订单
    	$implement_time=null;
    	$status=-1;
    	$order_id="";
    	switch ($db->type) {
    		case 1://充值
    			$implement_time=$db->aur_implement_time;
		    	$status=$db->aur_status;
		    	$order_id=$db->aur_order_id;
    			break;
    		case 2://订单支付
    			$implement_time=$db->o_implement_time;
		    	$status=$db->o_status;
		    	$order_id=$db->o_order_id;
    			break;
    		case 3://洗车单支付
    			$implement_time=$db->co_implement_time;
		    	$status=$db->co_status;
		    	$order_id=$db->co_order_id;
    			break;
    		case 4://订单收回
    			$implement_time=$db->r_implement_time;
		    	$status=$db->r_status;
		    	$order_id=$db->r_order_id;
    			break;
    		case 5://套餐卡支付
    			$implement_time=$db->so_implement_time;
		    	$status=$db->so_status;
		    	$order_id=$db->so_order_id;
    			break;
    	}
    	//处理业务订单完毕
    	//处理对账状态 和是否调用支付
    	
    	$pay_scene=$db->wpos_record_id?1:0;//如果存在系统对应的上 无相关记录 说明是
    	$end_status=0;
    	if($pay_scene==1){//说明 三方对账 
            if(($db->status==2||$db->status==5)&&$status==1&&$db->wpos_record_trade_status=="PAY"&&$db->own_pay_amount==$db->incomeAmt){//如果状态对的上 金额对的上
                $end_status=1;
            }else if($db->status==3&&$status==6&&$db->wpos_record_trade_status=="REFUND"){
                $end_status=1;
            }else{
                $end_status=0;
            }
            if($status==1){//说明业务订单状态正常
                $mdb_arr_amount[$db->deviceEn]['ye_amount']=$mdb_arr_amount[$db->deviceEn]['ye_amount']+$db->own_pay_amount;
            }
            if($db->wpos_record_trade_status=="PAY"){//说明wpos记录正常
                $mdb_arr_amount[$db->deviceEn]['pos_amount']=$mdb_arr_amount[$db->deviceEn]['pos_amount']+$db->own_pay_amount;
            }
        }else{//说明是非调用
            if($db->status==2||$db->status==5){//如果状态对的上
                $end_status=1;
            }else{
                $end_status=0;
            }
        }
        if($db->status==2||$db->status==5){
            $mdb_arr_amount[$db->deviceEn]['posdz_amount']=$mdb_arr_amount[$db->deviceEn]['posdz_amount']+$db->incomeAmt;
        }

    	$mdb_info=$mdb_arr[$db->deviceEn]; 
    	$fr->shop_id=$mdb_info->shop_id;
    	$fr->driver_id=$mdb_info->id;
    	$fr->device_name=$mdb_info->device_name;
    	$fr->device_no=$mdb_info->device_no;
        $fr->shop_no=isset($mdb_info->code)?$mdb_info->code:"";   
    	$fr->date=$c_date;
    	$fr->order_type=isset($db->type)?$db->type:0;
    	$fr->buss_id=isset($db->buss_id)?$db->buss_id:0;
    	$fr->ow_order_id=$order_id;
    	if(isset($implement_time)){
    		$fr->pay_time=$implement_time;
    	}    	
    	$fr->wpos_record_id=isset($db->wpos_record_id)?$db->wpos_record_id:0;
    	$fr->orderno=$db->orderno;
    	$fr->thirdChannelSerialno=$db->thirdChannelSerialno;
    	$fr->batchno=$db->batchno;
    	$fr->vouchno=$db->vouchno;
    	$fr->refno=$db->refno;
    	$fr->own_status=$status;
    	$fr->wpos_record_trade_status=isset($db->wpos_record_trade_status)?$db->wpos_record_trade_status:"";
    	$fr->pay_info=isset($db->pay_info)?$db->pay_info:"";
    	$fr->status=$db->status;
    	$fr->statusText=$db->statusText;
    	$fr->end_status=$end_status;
    	$fr->pay_scene=$pay_scene;
    	$fr->payModeName=$db->payModeName;
    	$fr->buyer=isset($db->buyer)?$db->buyer:"";
    	$fr->cardType=isset($db->cardType)?$db->cardType:"";
    	$fr->cardIssuresNo=$db->cardIssuresNo;
    	$fr->own_pay_amount=isset($db->own_pay_amount)?$db->own_pay_amount:0;
    	$fr->jy_amount=$db->payCash;
    	$fr->return_amount=$db->refundAmount;
    	$fr->end_amount=$db->incomeAmt;
    	$fr->save();
    }
}); 

foreach ($mdb_arr_amount as $key=>$_cd) {
    if($_cd["ye_amount"]==0&&$_cd["pos_amount"]==0&&$_cd["posdz_amount"]==0)
        continue;
    $frt=FinanceReconciliationsTotal::where("date",$c_date)->where("device_no",$key)->first();
    if(is_null($frt)){
        $frt=new FinanceReconciliationsTotal();
    }
    $mdb_info=$mdb_arr[$key]; 
    $frt->shop_id=$mdb_info->shop_id;
    $frt->driver_id=$mdb_info->id;
    $frt->device_name=$mdb_info->device_name;
    $frt->device_no=$mdb_info->device_no;
    $frt->shop_no=isset($mdb_info->code)?$mdb_info->code:"";  
    $frt->date=$c_date;
    $frt->ye_amount=$_cd["ye_amount"];
    $frt->pos_amount=$_cd["pos_amount"];
    $frt->dycx_amount=$_cd["pos_amount"]-$_cd["ye_amount"];
    $frt->posdz_amount=$_cd["posdz_amount"];
    $frt->fdy_amount=$_cd["posdz_amount"]-$_cd["pos_amount"];
    $frt->save();

}

